package com.winexpress.helper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encryptor {

    public static String md5(String password, byte[] salt) {
        byte[] digesta = null;
        byte[] passwordByte = password.getBytes();
        byte[] addSalt;
        addSalt = new byte[passwordByte.length + salt.length];
        for (int i = 0; i < addSalt.length; ++i) {
            addSalt[i] = i < passwordByte.length ? passwordByte[i] : salt[i - passwordByte.length];
        }
        try {
            MessageDigest alga = MessageDigest.getInstance("MD5");
            alga.update(addSalt);
            digesta = alga.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String hs = "";
        String stmp;
        for (int n = 0; n < digesta.length; n++) {
            stmp = (java.lang.Integer.toHexString(digesta[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

    public static String md5Bytes(byte[] msg) {
        byte[] digesta = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            digesta = md.digest(msg);
        } catch (NoSuchAlgorithmException e) {
        }
        String hs = "";
        String stmp;
        for (int n = 0; n < digesta.length; n++) {
            stmp = (java.lang.Integer.toHexString(digesta[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }

}
